import { getCurrentInstance } from "vue";

class CommonUtil {
  // 获取指定区间的随机数
  getRandomInt(min: number, max: number) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
  }

  // 设置localStorage
  setLocalStorage(key: string, value: any) {
    localStorage.setItem(key, JSON.stringify(value));
  }

  // 获取localStorage`
  getLocalStorage(key: string) {
    if (localStorage.getItem(key)) {
      return JSON.parse(localStorage.getItem(key)!)
    } else {
      return null
    }
  }

  // 深拷贝
  deepClone(obj: any) {
    let result: any = Array.isArray(obj) ? [] : {};
    for (let key in obj) {
      if (obj.hasOwnProperty(key)) {
        if (typeof obj[key] === 'object') {
          result[key] = this.deepClone(obj[key]);   //递归复制
        } else {
          result[key] = obj[key];
        }
      }
    }
    return result;
  }
  
  
  // 防抖函数
  debounce(func: Function, wait: number, immediate: boolean) {
    let timeout: any;
    return  () => {
      let context = this;
      let args = arguments;
      if (timeout) clearTimeout(timeout);
      if (immediate) {
        let callNow = !timeout;
        timeout = setTimeout(() => {
          timeout = null;
        }, wait);
        if (callNow) func.apply(context, args);
      } else {
        timeout = setTimeout(() => {
          func.apply(context, args);
        }, wait);
      }
    };
  }

  // 节流函数
  throttle(func: Function, wait: number, options: any) {
    let context: any = this;
    let args: any = arguments;
    let timeout: any;
    let previous = 0;
    if (!options) options = {};
    let later = function () {
      previous = options.leading === false ? 0 : Date.now();
      timeout = null;
      func.apply(context, args);
    };
    let throttled =  () => {
      let now = Date.now();
      if (!previous && options.leading === false) previous = now;
      let remaining = wait - (now - previous);
      context = this;
      args = arguments;
      if (remaining <= 0 || remaining > wait) {
        if (timeout) {
          clearTimeout(timeout);
          timeout = null;
        }
        previous = now;
        func.apply(context, args);
      } else if (!timeout && options.trailing !== false) {
        timeout = setTimeout(later, remaining);
      }
    };
    return throttled;
  }

  

  // 是否json格式
  isJSON(str: string) {
    if (typeof str == 'string') {
      try {
        let obj = JSON.parse(str);
        if (typeof obj == 'object' && obj) {
          return true;
        } else {
          return false;
        }
      } catch (e) {
        console.log('error：' + str + '!!!' + e);
        return false;
      }
    }
  }

  // 数组导出excel
  exportExcel(data: any[], fileName: string) {
    console.log(data);
    let str = ''
    for (let i = 0; i < data.length; i++) {
      for (let key in data[i]) {
        str += data[i][key] + ','
      }
      str += '\n'
    }
    let uri = 'data:text/csv;charset=utf-8,\ufeff' + encodeURIComponent(str)
    let link = document.createElement('a')
    link.href = uri
    link.download = fileName + '.csv'
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
  }


  

  getVue3GlobalAttribute(key: string) {
    return getCurrentInstance()?.appContext.config.globalProperties[key]
  }



}

export default new CommonUtil()